// pages/camera/card/card.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowCamera: false,
    isIphoneX: false,
    width: 10,
    height: 10,
    src: "",
    image: "",
    skipphotoStatus: "0", // 1跳过 0没有跳过
    isShowImage: false,
    isLookImage: false,
    barHeight: wx.getSystemInfoSync()['statusBarHeight']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // requireJs.adaptionIphoneX(this);
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowWidth)
        console.log(res.windowWidth - 30)
        // 设置高度
        that.setData({
          height: res.windowHeight,
          width: res.windowWidth,
          image_width: res.windowWidth - 30
        });
      }
    });
    this.ctx = wx.createCameraContext()

  },

  onShow: function () {
    var that = this
    if (!this.data.isLookImage) {
      wx.authorize({
        scope: 'scope.camera',
        success: function (res) {
          that.setData({
            isShowCamera: true,
          })
        },
        fail: function (res) {
          console.log("" + res);
          wx.showModal({
            title: '请求授权您的摄像头',
            content: '如需正常使用此小程序功能，请您按确定并在设置页面授权用户信息',
            confirmText: '确定',
            success: res => {
              if (res.confirm) {
                wx.openSetting({
                  success: function (res) {
                    console.log('成功');
                    console.log(res);
                    if (res.authSetting['scope.camera']) { //设置允许获取摄像头
                      console.log('设置允许获取摄像头')
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      that.setData({
                        isShowCamera: true,
                      })

                    } else { //不允许
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                      wx.redirectTo({
                        url: 'addCarInfo/addCarInfo',
                      })
                    }
                  }
                })
              } else { //取消
                wx.showToast({
                  title: '授权失败',
                  icon: 'none',
                  duration: 1000
                })
                wx.redirectTo({
                  url: 'addCarInfo/addCarInfo',
                })
              }
            }
          })

        }
      })
    }
  },

  // 返回
  goBack() {
    wx.navigateBack()
  },

  /**
   * 拍照
   */
  takePhotoAction: function () {
    var that = this
    that.ctx.takePhoto({
      quality: 'high', //高质量
      success: (res) => {
        this.loadTempImagePath(res.tempImagePath);
        console.log(res.tempImagePath);
        that.setData({
          isShowCamera: false,
          isShowImage: false,
          image: res.tempImagePath,
        })
      },
    })
  },

  loadTempImagePath: function (options) {
    var that = this
    that.path = options
    wx.getSystemInfo({
      success: function (res) {

        // 矩形的位置
        var image_x = 15;
        var image_y = 120;
        var image_width = that.data.width - 2 * 15;
        var image_height = 238;

        wx.getImageInfo({
          src: that.path,
          success: function (res) {
            that.setData({
              isShowImage: true,
            })
            that.canvas = wx.createCanvasContext("image-canvas", that)
            //过渡页面中，图片的路径坐标和大小
            that.canvas.drawImage(that.path, 0, 0, that.data.width, that.data.height)
            wx.showLoading({
              title: '数据处理中...',
              icon: 'loading',
              duration: 10000
            })
            // 这里有一些很神奇的操作,总结就是MD拍出来的照片规格居然不是统一的过渡页面中，对裁剪框的设定
            // that.canvas.setStrokeStyle('black')
            // that.canvas.strokeRect(image_x, image_y, image_width, image_height)
            that.canvas.draw()
            setTimeout(function () {
              wx.canvasToTempFilePath({ //裁剪对参数
                x: image_x, //画布x轴起点
                y: image_y, //画布y轴起点
                width: image_width, //画布宽度
                height: image_height, //画布高度
                destWidth: image_width, //输出图片宽度
                destHeight: image_height, //输出图片高度
                fileType: 'jpg',
                quality: 1,
                canvasId: "image-canvas",
                success: function (res) {
                  wx.setStorageSync('images', res.tempFilePath)
                  that.setData({
                    isShowCamera: false,
                    isShowImage: false,
                    cardImage: res.tempFilePath,
                  })
                  //清除画布上在该矩形区域内的内容。
                  // that.canvas.clearRect(0, 0, that.data.width, that.data.height)
                  // that.canvas.drawImage(res.tempFilePath, image_x, image_y, image_width, image_height)
                  // that.canvas.draw()
                  wx.hideLoading()

                  console.log(res.tempFilePath);
                  //在此可进行网络请求
                  // PublicJS.drivinglicenseUpload(res.tempFilePath, that.uploadFile);
                },
                fail: function (e) {
                  wx.hideLoading()
                  wx.showToast({
                    title: '出错啦...',
                    icon: 'loading'
                  })
                  if (this.data.skipphotoStatus == 1) {
                    wx.redirectTo({
                      url: 'addCarInfo/addCarInfo',
                    })
                  } else {
                    wx.navigateBack({
                      delta: 1
                    });
                  }
                }
              });
            }, 1000);
          }
        })
      }
    })
  },

  // 预览图片
  previewImg(e) {
    this.data.isLookImage = true
    //所有图片
    var cardImage = this.data.cardImage;
    wx.previewImage({
      //当前显示图片
      current: cardImage,
      //所有图片
      urls: [cardImage]
    })
  },

  // 重新拍摄
  cancelBtn() {
    this.setData({
      isShowCamera: true
    })
  },

  // 确定
  confirmBnt() {
    that.setData({
      isShowCamera: true
    })
  },

})